﻿using System.Collections.Generic;
using System.Data.SqlClient;

namespace BLL.Services.Authors
{
    using BLL.Services.Authors.Listing;

    public class AuthorsService : IAuthorsService
    {
        private readonly AuthorsCollection _authorsCollection;

        /// <summary>
        /// Initializes a new instance of the <see cref="AuthorsService" /> class.
        /// </summary>
        /// <param name="collection">The collection.</param>
        public AuthorsService(AuthorsCollection collection)
        {
            _authorsCollection = collection;
        }


        /// <summary>
        /// Gets the author by id.
        /// </summary>
        /// <param name="authorId">The author id.</param>
        public Author GetAuthorById(int authorId)
        {
            try
            {
                return _authorsCollection.SelectById(authorId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Creates the specified author.
        /// </summary>
        /// <param name="author">The author.</param>
        public void Create(Author author)
        {
            try
            {
                _authorsCollection.Create(author);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Saves the specified author.
        /// </summary>
        /// <param name="author">The author.</param>
        public void Save(Author author)
        {
            try
            {
                _authorsCollection.Save(author);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Deletes the specified author id.
        /// </summary>
        /// <param name="authorId">The author id.</param>
        public void Delete(int authorId)
        {
            try
            {
                _authorsCollection.Delete(authorId);
            }
            catch (SqlException sqlException)
            {
                throw new ServiceException(sqlException.Message);
            }
        }

        /// <summary>
        /// Gets the listing author.
        /// </summary>
        /// <param name="page">The page.</param>
        /// <param name="size">The size.</param>
        /// <param name="column">The column.</param>
        /// <param name="direction">The direction.</param>
        /// <param name="filter">The filter.</param>
        public AuthorListing GetListingAuthor(
            int page, int size, string column, int direction, AuthorFilter filter)
        {
            try
            {
                return _authorsCollection.GetListingAuthor(page, size, column, direction, filter);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            }
        }

        /// <summary>
        /// Validates the specified author id.
        /// </summary>
        /// <param name="authorId">The author id.</param>
        /// <param name="firstName">The first name.</param>
        /// <param name="lastName">The last name.</param>
        /// <param name="middleName">Name of the middle.</param>
        public bool ValidateAuthor(int authorId, string firstName, string lastName, string middleName)
        {
            try
            {
                return _authorsCollection.Validate(authorId, firstName, lastName, middleName);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            }
        }

        public IEnumerable<Author> GetAuthorsByLetter(string letter)
        {
            try
            {
                return _authorsCollection.GetAuthorsByLetter(letter);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            } 
        }

        public IEnumerable<Author> GetAuthorsByIds(int[] authorsIds)
        {
            try
            {
                return _authorsCollection.GetAuthotsByIds(authorsIds);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            } 
        }
        
        public int GetAuthorId(string firstName, string lastname, string middlenName)
        {
            try
            {
                return _authorsCollection.GetAuthorId(firstName, lastname, middlenName);
            }
            catch (SqlException ex)
            {
                throw new ServiceException(ex.Message);
            } 
        }
    }
}
